/***********************************************************************************
Copy right:	    Coffee Tech.
Author:         jiaoyue
Date:           2021-11-21
Description:    设备采集进程主函数
***********************************************************************************/

#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <log_utils.h>
#include <nv_utils.h>
#include "dc_pub.h"
#include "dc_service.h"

#define MODULE DC_NAME

#define SUB_THREAD_SUM 2 //子线程数

int main(int argc, char const *argv[])
{
    pthread_t tid[SUB_THREAD_SUM];
    int ret;

    //初始化log服务
    set_dbg_level(APP_DEBUG);

    //将进程作为中心型服务，初始化unix_sock
    if(init_dc_service() < 0)
    {
        return -1;
    }

    //启动m0数据采集线程
    ret = pthread_create(&tid[0], NULL, m0_collect_thread, NULL);
    if (ret != 0)
    {
        LOG_STD(APP_ERR, "m0_collect_thread err");
        return -1;
    }

    //启动指令处理线程
    ret = pthread_create(&tid[1], NULL, msg_handle_thread, NULL);
    if (ret != 0)
    {
        LOG_STD(APP_ERR, "client_msg_handle_thread err");
        return -1;
    }

    int kill_rc;

    while (1)
    {
        //维护子线程
        for (int i = 0; i < SUB_THREAD_SUM; i++)
        {
            kill_rc = pthread_kill(tid[i], 0);
            if (kill_rc == ESRCH || kill_rc == EINVAL)
            {
                printf("sub thread[%d] is dead, process will quit and restart now", i);
                exit(-1);
            }
        }
        sleep(3);
    }


    return 0;
}
